<template>
    <div class="guide-mask" v-if="currentStep < steps.length">
      <!-- 黑色遮罩 -->
      <div class="guide-overlay"></div>
      
      <!-- 高亮区域 -->
      <div 
        class="highlight-area"
        :style="highlightStyle"
      ></div>
      
      <!-- 引导内容 -->
      <div 
        class="guide-content"
        :style="contentStyle"
      >
        <div class="guide-arrow"></div>
        <div class="guide-inner">
          <h3>{{ steps[currentStep].title }}</h3>
          <p>{{ steps[currentStep].content }}</p>
          <div class="guide-buttons">
            <el-button v-if="currentStep > 0" @click="prevStep" size="small">上一步</el-button>
            <el-button type="primary" @click="nextStep" size="small">
              {{ currentStep === steps.length - 1 ? '完成' : '下一步' }}
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, computed, onMounted, nextTick } from 'vue'
  
  const emit = defineEmits(['guide-completed'])
  const currentStep = ref(0)
  
  // 查找菜单项并生成步骤
  const findMenuSteps = () => {
    const steps = []
    const menuItems = document.querySelectorAll('.el-menu-item, .el-sub-menu__title')
    
    menuItems.forEach(item => {
      const text = item.textContent.trim()
      if (text == '账户管理') {
        const rect = item.getBoundingClientRect()
        steps.push({
          title: text,
          content: `这里是${text}功能，如果您要发布视频，请先在账户平台里面添加您的账户。`,
          element: item,
          rect: rect
        })
      }
      if (text == '个人中心') {
        const rect = item.getBoundingClientRect()
        steps.push({
          title: text,
          content: `这里是${text}功能，您可以在此页面编辑您的个人信息。`,
          element: item,
          rect: rect
        })
      }
      if (text == '媒体管理') {
        const rect = item.getBoundingClientRect()
        steps.push({
          title: text,
          content: `这里是${text}功能，此功能帮助发布您的视频。`,
          element: item,
          rect: rect
        })
      }
    })
    
    return steps
  }
  
  const steps = ref([])
  
  // 初始化步骤
  const startGuide = (async () => {
    await nextTick()
    steps.value = findMenuSteps()
  })

  defineExpose({
    startGuide
  })
  
  // 计算高亮区域样式
  const highlightStyle = computed(() => {
    if (!steps.value[currentStep.value]) return {}
    
    const rect = steps.value[currentStep.value].rect
    const padding = 4
    
    return {
      top: `${rect.top - padding}px`,
      left: `${rect.left - padding}px`,
      width: `${rect.width + padding * 2}px`,
      height: `${rect.height + padding * 2}px`,
      color: '#409EFF', // 高亮文字颜色
      display: 'flex',
      alignItems: 'center',
    }
  })
  
  // 计算提示内容位置
  const contentStyle = computed(() => {
    if (!steps.value[currentStep.value]) return {}
    
    const rect = steps.value[currentStep.value].rect
    const offset = 20
    
    return {
      position: 'fixed',
      left: `${rect.right + offset}px`,
      top: `${rect.top + rect.height / 2}px`,
      transform: 'translateY(-50%)'
    }
  })
  
  const nextStep = () => {
    if (currentStep.value < steps.value.length - 1) {
      currentStep.value++
    } else {
      emit('guide-completed')
    }
  }
  
  const prevStep = () => {
    if (currentStep.value > 0) {
      currentStep.value--
    }
  }
  </script>
  
  <style scoped>
  .guide-mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 9999;
  }
  
  .guide-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
  
  .highlight-area {
    position: absolute;
    z-index: 10000;
    background: transparent;
    border: 2px solid #409EFF;
    border-radius: 4px;
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.75);
    transition: all 0.3s ease;
    pointer-events: none;
  }
  
  .guide-content {
    position: fixed;
    z-index: 10001;
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
  }
  
  .guide-inner {
    padding: 20px;
    width: 280px;
  }
  
  .guide-arrow {
    position: absolute;
    left: -6px;
    top: 50%;
    width: 12px;
    height: 12px;
    background: white;
    transform: translateY(-50%) rotate(45deg);
  }
  
  h3 {
    margin: 0 0 10px;
    font-size: 16px;
    color: #303133;
  }
  
  p {
    margin: 0 0 15px;
    font-size: 14px;
    color: #606266;
    line-height: 1.5;
  }
  
  .guide-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
  }
  </style>